<?php

namespace App\Providers;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
        Schema::defaultStringLength(191);
        if (config('app.debug')) {
            DB::listen(function ($query) {
                $tmp = str_replace('?', "'" . '%s' . "'", $query->sql);
                //避免报错Object of class DateTime could not be converted to string
                $bindings = array_map(function ($binding) {
                    if ($binding instanceof \DateTime) {
                        return $binding->format('Y-m-d H:i:s'); // 按照数据库的格式要求来调整
                    }
                    return $binding;
                }, $query->bindings);
                $tmp = vsprintf($tmp, $bindings);
                $tmp = str_replace("\\", "", $tmp);
                Log::info(' execution time: ' . $query->time . 'ms; ' . $tmp . "\n\n\t");
            });
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
